package org.ala.tiktools.mybatis.expand;

import org.ala.tiktools.mybatis.expand.binding.MapperMethodExpander;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 持有Method和MapperMethodExpander对应关系，后续可能会用到
 *
 * @author ala
 * @date 2025-03-12 14:35
 */
public class MapperMethodHolder {
    /**
     *  method和mapperMethod对应
     */
    protected Map<Method, MapperMethodExpander> holder = new ConcurrentHashMap<>();
    /**
     *  config
     */
    protected ConfigurationExpander config;


    public MapperMethodHolder(ConfigurationExpander config) {
        this.config = config;
    }


    /**
     *  追加
     */
    public void put(Method method, MapperMethodExpander expander) {
        holder.put(method, expander);
    }

    /**
     *  返回扩展的MapperMethod
     */
    public MapperMethodExpander cachedMapperMethod(Class<?> mapperInterface, Method method) {
        MapperMethodExpander mapperMethod = holder.get(method);
        if (mapperMethod == null) {
            mapperMethod = new MapperMethodExpander(mapperInterface, method, config);
            holder.put(method, mapperMethod);
        }
        return mapperMethod;
    }

}
